#include <stdio.h>
#include <stdlib.h>

#include "queue.h"
/*
 * 队列 
 * 规定:
 * 	head 位置不能有元素
 * 	tail 位置为最后一元素
 * 		即 有效存储空间 为 MAXSIZE -1
 * 	空:head == tail;
 * 	满:(tail + 1) %MAXSIZE = head;
 * 		注意 遍历时的操作
 * */
queue *qu_create()
{
	queue *sq;
	sq = malloc(sizeof(*sq));
	if (sq == NULL)
		return NULL;

	sq->head = 0;
	sq->tail = 0;
	
	return sq;
}

int qu_isempty(queue *sq)
{
	if (sq->head == sq->tail)
		return 0;
	return -1;
}

int qu_enqueue(queue *sq,datatype *x)
{
	if ((sq->tail + 1) % MAXSIZE == sq->head)
		return -1;

	sq->tail = (sq->tail + 1) % MAXSIZE;
	sq->data[sq->tail] = *x;
	return 0;
}

int qu_dequeue(queue *sq,datatype *x)
{
	if(qu_isempty(sq) == 0)
		return -1;
	sq->head = (sq->head + 1) %MAXSIZE;
	*x = sq->data[sq->head];
	return 0;
}

void qu_travel(queue *sq)
{
	if (qu_isempty(sq) == 0)
		return ;
	int i = (sq->head+ 1) %MAXSIZE;
	while(i != sq->tail)
	{
		printf("%d ",sq->data[i]);
		i = (i + 1)%MAXSIZE;
	}
	printf("%d\n",sq->data[i]);
	return ;
}

void qu_clear(queue *sq)
{
	sq->head = sq->tail;
}

void qu_destory(queue *sq)
{
	free(sq);
}
